package org.example.wu_and_dian.controller;

import org.example.wu_and_dian.annotation.TokenRequired;
import org.example.wu_and_dian.dto.request.DocumentRequest;
import org.example.wu_and_dian.dto.request.UpdateDocument;
import org.example.wu_and_dian.dto.request.UpdateDocumentContent;
import org.example.wu_and_dian.dto.response.AjaxResult;
import org.example.wu_and_dian.dto.response.DocumentResponse;
import org.example.wu_and_dian.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.wu_and_dian.entity.Document;

import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

@RestController
public class DocumentController {
    @Autowired
    private DocumentService documentService;
    @TokenRequired
    @GetMapping("/get_document_list")
    public AjaxResult getDocumentList(@RequestParam("projectId") String projectId) {
        System.out.println(projectId);
        List<DocumentResponse> documents = documentService.getDocumentsByProjectId(projectId);
        System.out.println(documents);
        return AjaxResult.success(documents);
    }
    @TokenRequired
    @PostMapping("/create_document")
    public AjaxResult createDocument(@RequestBody DocumentRequest documentRequest) {
        try {
            AjaxResult result = documentService.createDocument(documentRequest);
            return result;
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @TokenRequired
    @DeleteMapping("/document/delete/{documentId}")
    public AjaxResult deleteDocument(@PathVariable("documentId") String documentId) {
        int deletedRows = documentService.deleteDocument(documentId);
        return new AjaxResult()
                .put("success", deletedRows > 0)
                .put("message", deletedRows > 0 ? "删除成功" : "文档不存在")
                .put("num", deletedRows);
    }
    @TokenRequired
    @PutMapping("/update_document")
    public AjaxResult updateDocument( @RequestBody UpdateDocument updateDocument) {
        return documentService.updateDocument(updateDocument);
    }
    @TokenRequired
    @PostMapping("/update_document_content")
    public AjaxResult updateDocumentContent(@RequestBody UpdateDocumentContent updateDocument) throws ParseException {
        String documentId = updateDocument.getDocumentId();
        String content = updateDocument.getContent();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date updatedAt = sdf.parse(sdf.format(new Date()));
        return documentService.updateDocumentContent(documentId, content, updatedAt);
    }
    @TokenRequired
    @PostMapping("/document/upload")
    public AjaxResult uploadDocument(@RequestBody DocumentRequest documentRequest) {
        try {
            AjaxResult result = documentService.uploadDocument(documentRequest);
            return result;
        } catch (IllegalArgumentException e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @TokenRequired
    @PostMapping("/get_document")
    public AjaxResult getDocument(@RequestBody Map<String, String> requestBody) {
        String documentId = requestBody.get("documentId");
        Document document = documentService.getDocument(documentId);
        if (document != null) {
            return AjaxResult.success(Map.of("document", document)).put("message", "获取文档成功");
        }
        return AjaxResult.error("文档不存在");
    }
    @TokenRequired
    @PostMapping("/update_document_author")
    public AjaxResult updateDocumentAuthor(@RequestBody Map<String, String> requestBody) {
        String documentId = requestBody.get("documentId");
        String author = requestBody.get("author");
        Document document = new Document();
        document.setId(documentId);
        document.setAuthor(author);
        int updatedRows = documentService.updateContentAuthorById(document);
        return new AjaxResult()
                .put("success", updatedRows > 0)
                .put("message", updatedRows > 0 ?"更新成功" : "文档不存在");
    }
}
